#include "GlobalPrec.h"

USING_NS_CC;

/*
void GameLayerMenu::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
	CCMenu::ccTouchEnded(touch,event);
	if(m_pSelectedItem)
	{
		mParent->Commond(m_pSelectedItem,event);
	}
}*/

bool GameLayer::init()
{
	if(!CCLayer::init())
		return false;
	
	if(!OnInit())
		return false;

	return true;
}

void GameLayer::AddChild(CSString name,CCNode* child,int z)
{
	if(mUIItems.Found(name))
		removeChild(mUIItems[name],true);
	mUIItems.Insert(name,child);

	CCMenuItem* menuitem = dynamic_cast<CCMenuItem*>(child);
	if(menuitem)
	{
		if(!mMenu)
		{
			mMenu=new CCMenu();
			mMenu->autorelease();
			mMenu->init();
			mMenu->setAnchorPoint(CCPointZero);
			mMenu->setPosition(CCPointZero);
			mMenu->setContentSize(getContentSize());
			addChild(mMenu,5);
		}
		mMenu->addChild(child,z);
	}
	else
	{
		addChild(child,z);
	}
	
}

void GameLayer::AddChild(GameLayerConfigElem* elem,CCNode* child)
{
	AddChild(elem->mName,child,elem->mZOrder);
}

bool GameLayer::IsMatch(CSString name,CCNode* node)
{
	CSHashMap<CSString,CCNode*>::iterator iter=mUIItems.Find(name);
	if(iter==mUIItems.End())
		return false;
	return (iter->second==node?true:false);
}

bool GameLayer::Commond(CCNode* sender,CSString event)
{
	return true;
}

void CCMenuButton::init(GameLayerConfigButton* elem)
{
	CCSprite* normalSpe = 0;
	CCSprite* selectedSpe = 0;
	CCSprite* disableSpe = 0;
	if(elem->mNormalImage!="")
		normalSpe = CCSprite::create(*elem->mNormalImage);
	if(elem->mSelectedImage!="")
		selectedSpe = CCSprite::create(*elem->mSelectedImage);
	if(elem->mDisabledImage!="")
		disableSpe = CCSprite::create(*elem->mDisabledImage);
	if(!initWithNormalSprite(normalSpe,selectedSpe,disableSpe,0,0))
		return ;
	mTxtLabel = CCLabelTTF::create(*elem->mText,*elem->mFont,elem->mFontSize);
	addChild(mTxtLabel);
}

void CCMenuButton::SetCaption(CSString str)
{
	mTxtLabel->setString(*str);
}

void CCMenuButton::activate()
{
	CCMenuItemSprite::activate();
	CCNode* menu = getParent();
	if(!menu)
		return ;
	
	GameLayer* gamelayer = dynamic_cast<GameLayer*>(menu->getParent());
	if(gamelayer)
		gamelayer->Commond(this,"click");
	
}